<template>
  <div id="infops" style="margin-top:70px">
    <el-row :gutter="7">
      <el-col :span="7" :offset="5">
        <el-form 
          :model="userResiterForm"
          status-icon
          :rules="rules"
          ref="userResiterForm"
          label-width="100px"
          class="demo-userResiterForm"
        >
          <el-form-item label="原密码" prop="password">
            <el-input  v-model="userResiterForm.password"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="oldpassword">
            <el-input type="password" v-model="userResiterForm.oldpassword" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="checkpassword">
            <el-input type="password" v-model="userResiterForm.checkpassword" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="infoSubmitForm('userResiterForm')">提交</el-button>
            <el-button @click="resetForm('userResiterForm')">重置</el-button>
          </el-form-item>
          <!-- </el-col>
          </el-row>-->
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else {
        if (this.userResiterForm.checkpassword !== "") {
          this.$refs.userResiterForm.validateField("checkpassword");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else if (value !== this.userResiterForm.oldpassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      activeforbt: false,
      usernamecheck: "",
      userResiterForm: {
        userId:'',
        password: "",
        checkpassword: "",
      },
      userResitersjForm: {},
    
      rules: {
        password: [
          { required: true, message: "请输入原密码", trigger: "blur" },
          
        
        ],
        oldpassword: [
         
          { validator: validatePass, trigger: "blur" ,message: "请输入新密码"}
        ],
       
        checkpassword: [{ validator: validatePass2, trigger: "blur" }],
      }
    };
  },
  methods: {
    create() {
      this.activename = "first";
    },
    //修改密码
    infoSubmitForm(formName) {
      console.log(this.userResiterForm)
      this.$refs[formName].validate(valid => {
        let userid = localStorage.getItem("loginUser")
          if (valid) {
            this.userResiterForm.userId=userid;
            
            this.$http
              .post("/updatePassword", this.userResiterForm)
              .then(response => {
                if ((response.status = 200 && response.data.success)) {
                  this.$message({
                    message: "修改成功",
                    type: "success"
                  });
                } else {
                  this.$message({
                    message: "修改失败",
                    type: "error"
                  });
                }
              })
              .catch(error => {
                this.$message({
                  message: "修改失败",
                  type: "error"
                });
              });
          } else {
            return false;
          }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
 
  }
};
</script>